package com.agent.tool;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class AjaxReturnTool {
	Properties properties = new Properties();
    Map<String,Object> map;
    
    public Map<String, Object> getMap() {
		return map;
	}
    public AjaxReturnTool fail(){
    	return fail("-10001","");
    }
	public AjaxReturnTool fail(int key){
    	return fail(key+"");
    }
	public AjaxReturnTool fail(String key){
		System.out.println(key);
    	return fail(key,"");
    }
    public AjaxReturnTool fail(String key,Object obj){
   	 	this.map= new HashMap<String,Object>();
		this.map.put("code", key);
		this.map.put("msg", findDesc(key));
		this.map.put("content", obj);
		return this;
    }
    
    public AjaxReturnTool success(){
    	return success("");
    }
    
    public AjaxReturnTool success(Object obj){
   	 	this.map= new HashMap<String,Object>();
    	this.map.put("code", "200");
    	this.map.put("msg", "success");
    	this.map.put("content", obj);
		return this;
    }
    
    public String findDesc(String key){
    	Properties properties = new Properties();
        try {
			properties.load(AjaxReturnTool.class
			        .getResourceAsStream("/com/travel/exception/exception.properties"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return PropertiesTool.get(properties, key);
    }
	public String toJson() {
		Gson gson = new GsonBuilder().serializeNulls().create();
		return gson.toJson(this.map);
	}
}
